package com.ruoyi.app.controller;

import com.ruoyi.app.dto.MemberCarDto;
import com.ruoyi.app.dto.MemberFeedbackDto;
import com.ruoyi.app.service.IAppMemberService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.domain.MemberCar;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.service.IMemberCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * App端 -  会员相关
 */
@CrossOrigin
@RestController
@RequestMapping("/app")
public class AppMemberController extends BaseController {

    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IAppMemberService memberService;
    @Autowired
    private IMemberCarService memberCarService;

    /**
     *  用户自动登录
     * @param code 通过code获取openid自动登录
     * @return
     */
    @GetMapping("/autoLogin")
    public AjaxResult autoLogin(String code){
        return success(memberService.autoLogin(code));
    }

    /**
     * 我的停车卡
     * 后期换成用户id
     * @return
     */
    @GetMapping("/getMyParkCard")
    public AjaxResult getMyParkCard(String uniqueId){
        return success(memberService.getMyParkCard(uniqueId));
    }


    /**
     * 提交意见反馈
     * @return
     */
    @PostMapping("/addFeedback")
    public AjaxResult addFeedback(@RequestBody MemberFeedbackDto memberFeedbackDto){
        return success(memberService.addFeedback(memberFeedbackDto));
    }
    /**
     * 获取会员车辆列表
     * @return
     */
    @GetMapping("/getMemberCarList")
    public AjaxResult getMemberCarList(Long memberId){
        return success(memberCarService.selectMemberCarListByMemberId(memberId));
    }
    /**
     * 添加会员车辆
     * @return
     */
    @PostMapping("/addMemberCar")
    public AjaxResult addMemberCar(@RequestBody MemberCarDto memberCarDto){
        return success(memberService.addMemberCar(memberCarDto));
    }
    /**
     * 删除会员车辆
     * @return
     */
    @PostMapping("/deleteMemberCar")
    public AjaxResult deleteMemberCar(@RequestBody MemberCarDto memberCarDto){
        return success(memberCarService.deleteMemberCarById(memberCarDto.getId()));
    }
    /**
     * 更新会员信息-认证信息
     * @return
     */
    @PostMapping("/updateMemberCar")
    public AjaxResult updateMemberCar(@RequestBody MemberCar memberCar){
        memberCar.setAuthStatus("1");
        return success(memberCarService.updateMemberCar(memberCar));
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file){
        try{
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }
}
